package com.example.momo_01.adapter;

import java.util.List;

import com.example.momo_01.activity.R;
import com.example.momo_01.debug.util.DebugTool;
import com.example.momo_01.view.ViewPagerFragment;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

public class ViewPagerAdapter extends PagerAdapter implements
		ViewPager.OnPageChangeListener {
	private List<Fragment> fragments; // ÿ��Fragment��Ӧһ��Page
	private FragmentManager fragmentManager; // fragmentManager����
	private ViewPager viewPager; // viewPager����
	private int currentPageIndex = 0; // ��ǰpage�����л�֮ǰ��

	public ViewPagerAdapter(List<Fragment> fragments,
			FragmentManager fragmentManager, ViewPager viewPager) {
		super();
		this.fragments = fragments;
		this.fragmentManager = fragmentManager;
		this.viewPager = viewPager;

		this.viewPager.setAdapter(this);
		this.viewPager.setOnPageChangeListener(this);
	}

	@Override
	public int getCount() {
		return fragments.size();
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == arg1;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		Fragment fragment = fragments.get(position) ;

		//DebugTool.syso(getClass(), "instantiateItem&&&&&&fragment.isAdded():"+fragment.isAdded());
		
		if (!fragment.isAdded()) { // ���fragment��û��added
			FragmentTransaction ft = fragmentManager.beginTransaction();
			ft.add(fragment, fragment.getClass().getName() + position);
			ft.commit();
			/**
			 * ����FragmentTransaction.commit()�����ύFragmentTransaction�����
			 * ���ڽ�̵����߳��У����첽�ķ�ʽ��ִ�С� �����Ҫ����ִ������ȴ��еĲ�������Ҫ�������������ֻ�������߳��е��ã���
			 * Ҫע����ǣ����еĻص�����ص���Ϊ��������������б�ִ����ɣ����Ҫ��ϸȷ����������ĵ���λ�á�
			 */
			fragmentManager.executePendingTransactions();
		}
		if (fragment.getView().getParent() == null) {
			container.addView(fragment.getView()); // Ϊviewpager���Ӳ���
		}
		return fragment.getView();
	}

	/**
	 * ��ǰpage�����л�֮ǰ��
	 * 
	 * @return
	 */
	public int getCurrentPageIndex() {
		return currentPageIndex;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		// �Ƴ�viewpager����֮���page����
		container.removeView(fragments.get(position).getView());
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
//		DebugTool.syso(getClass(), arg0+"");
//		DebugTool.syso(getClass(), "fragments.get(arg0).isAdded():"+fragments.get(arg0).isAdded());
		
		fragments.get(currentPageIndex).onPause(); // �����л�ǰFargment��onPause()
		DebugTool.syso(getClass(), "fragments.get(arg0).isAdded():"+fragments.get(arg0).isAdded());
		if(fragments.get(arg0).isAdded()){
			fragments.get(arg0).onResume(); // �����л���Fargment��onResume()
		}
		
		currentPageIndex = arg0;
	}

}
